<template>
  <el-container class="home_contain">
    <!-- 头部区域 -->
    <el-header>
      <div class="header_left">
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">登出</el-button>
    </el-header>
    <!-- 内容主体部分 -->
    <el-container>
      <!-- 侧边栏部分 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="toggleCollaps" @click="toggleCollaps">|||</div>
        <el-menu
          :collapse="iscollapse"
          :collapse-transition="false"
          unique-opened
          router
          :default-active="defaultActive"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF">
          <!-- 一级菜单 -->
          <el-submenu
            v-for="(menu, num) in menusData"
            :key="menu.id"
            :index="num + ''">
            <template slot="title">
              <i :class="'iconfont' + ' ' + iconList[num]"></i>
              <span>{{menu.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="menuChil in menu.children" :key="menuChil.id" :index="menuChil.path" @click="setDefaultActive(menuChil.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{menuChil.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容显示部分 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data () {
    return {
      menusData: [],
      iconList: ['icon-users', 'icon-tijikongjian', 'icon-shangpin', 'icon-danju', 'icon-baobiao'],
      iscollapse: false,
      defaultActive: ''
    }
  },
  methods: {
    // 登出
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取侧边栏项目的数据
    async getMenuData () {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menusData = res.data
    },
    // 控制侧边栏折叠与否
    toggleCollaps () {
      this.iscollapse = !this.iscollapse
    },
    // 存储需要高亮的那个二级菜单项目
    setDefaultActive (path) {
      window.sessionStorage.setItem('defaultActive', path)
      this.defaultActive = path
    }
  },
  created () {
    this.getMenuData()
    this.defaultActive = window.sessionStorage.getItem('defaultActive')
  }
}
</script>

<style scoped>
.el-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 0;
  background-color: #373d41;
  font-size: 20px;
  color: #fff;
}
.el-header .header_left {
  display: flex;
  align-items: center;
}
.el-header .header_left span {
  margin-left: 15px;
}

.el-aside {
  background-color: #333744;
}

.el-main {
  background-color: #EAEDF1;
}

.home_contain {
  height: 100%;
}

.iconfont {
  margin-right: 10px;
}

.el-menu {
  border-right: 0;
}

.toggleCollaps {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
